import axios from 'axios'
import router from './router'
import {useStore} from './store';

axios.defaults.timeout = 30e3;  //请求30秒超时

//响应处理
axios.interceptors.response.use(res => {
  if (!res.data) {
    return res
  }
  let rawData = res.data;
  if (rawData.code === 200) {
    return rawData.data;
  } else if (rawData.code === 403) {
    useStore().setLogout();
    router.push({path: '/login'});
    return null;
  } else {
    return Promise.reject(rawData.description)
  }
}, error => {
  return Promise.reject(error.response?.data?.description || error.message)
});

export const getTokenByAccount = (params: any) => axios.get<any, any>('/api/getTokenByAccount', {params});
export const getUserInfo = () => axios.get<any, any>('/api/getUserInfo');
export const getTotalData = () => axios.get<any, any>('/api/getTotalData');
export const getEchartsData = () => axios.get<any, any>('/api/getEchartsData');

export const getDinkTalkConfigList = (params: any) => axios.get<any, any>('/api/getDinkTalkConfigList', {params});
export const saveDinkTalkConfig = (params: any) => axios.post<any, any>('/api/saveDinkTalkConfig', params);
export const sendDingTalkTest = (params: any) => axios.get<any, any>('/api/sendDingTalkTest', {params});
export const getMysqlConfigList = (params: any) => axios.get<any, any>('/api/getMysqlConfigList', {params});
export const saveMysqlConfig = (params: any) => axios.post<any, any>('/api/saveMysqlConfig', params);
export const sendMysqlTest = (params: any) => axios.get<any, any>('/api/sendMysqlTest', {params});

export const getUserPage = (params: any) => axios.get<any, any>('/api/getUserPage', {params});
